package com.ohuang.wallpapermanager.screen

import androidx.compose.runtime.MutableState
import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.runtime.toMutableStateList
import com.ohuang.wallpapermanager.compose.toMutableState
import com.ohuang.wallpapermanager.data.TimeData
import com.ohuang.wallpapermanager.data.TimeFrameData
import com.ohuang.wallpapermanager.data.WallPaperInfoTimeRuleData
import com.ohuang.wallpapermanager.data.WallPaperInfoWeekData



data class EditWallPaperInfoState(
    val id: Long = 0L,
    val filePath: String = "", val uri: String = "",
    val isLoading: Boolean = false,//是否正在加载界面
    val timeRule: EditWallPaperInfoTimeRuleState = EditWallPaperInfoTimeRuleState(),
    val isForceShow: MutableState<Boolean> = mutableStateOf(true),
)

fun EditWallPaperInfoState.toData(): com.ohuang.wallpapermanager.data.WallPaperInfoData {
    return com.ohuang.wallpapermanager.data.WallPaperInfoData(
        id = id,
        filePath = filePath,
        timeRule = timeRule.toData(),
        isForceShow = isForceShow.value,
    )
}


fun com.ohuang.wallpapermanager.data.WallPaperInfoData.toState(): EditWallPaperInfoState {
    return EditWallPaperInfoState(
        id = id,
        filePath = filePath,
        timeRule = timeRule.toState(),
        isForceShow = isForceShow.toMutableState()
    )
}


data class EditWallPaperInfoTimeRuleState(
    val timeFrame: TimeFrameState = TimeFrameState(),
    val week: EditWallPaperInfoWeekState = EditWallPaperInfoWeekState()
)

fun WallPaperInfoTimeRuleData.toState(): EditWallPaperInfoTimeRuleState {
    return EditWallPaperInfoTimeRuleState(timeFrame = timeFrame.toState(), week = week.toState())
}

fun EditWallPaperInfoTimeRuleState.toData(): WallPaperInfoTimeRuleData {
    return WallPaperInfoTimeRuleData(
        timeFrame = timeFrame.toData(),
        week = week.toData()
    )
}


data class TimeFrameState(
    val antTimeFrame: MutableState<Boolean> = mutableStateOf(true),
    val startTime: MutableState<EditWallPaperInfoTimeState> = mutableStateOf(
        EditWallPaperInfoTimeState()
    ),
    val endTime: MutableState<EditWallPaperInfoTimeState> = mutableStateOf(
        EditWallPaperInfoTimeState()
    )
)

fun TimeFrameState.toData(): TimeFrameData {
    return TimeFrameData(
        antTimeFrame = antTimeFrame.value,
        startTime = startTime.value.toData(),
        endTime = endTime.value.toData()
    )
}

fun TimeFrameData.toState(): TimeFrameState {
    return TimeFrameState(
        antTimeFrame = antTimeFrame.toMutableState(),
        startTime = startTime.toState().toMutableState(),
        endTime = endTime.toState().toMutableState()
    )
}


data class EditWallPaperInfoTimeState(val hour: Int = 0, val minute: Int = 0)

fun EditWallPaperInfoTimeState.toData(): TimeData {
    return TimeData(hour = hour % 24, minute = minute % 60)
}

fun TimeData.toState(): EditWallPaperInfoTimeState {
    return EditWallPaperInfoTimeState(hour = hour, minute = minute)
}


data class EditWallPaperInfoWeekState(
    val anyDay: MutableState<Boolean> = mutableStateOf(true),
    val weekTips: SnapshotStateList<String> =
        mutableStateListOf("周日", "周一", "周二", "周三", "周四", "周五", "周六"),
    val weekDayList: SnapshotStateList<Int> = mutableStateListOf(),
)

fun WallPaperInfoWeekData.toState(): EditWallPaperInfoWeekState {
    return EditWallPaperInfoWeekState(
        anyDay = this.anyDay.toMutableState(),
        weekDayList = weekDayList.toMutableStateList()
    )
}

fun EditWallPaperInfoWeekState.toData(): WallPaperInfoWeekData {
    return WallPaperInfoWeekData(anyDay = anyDay.value, weekDayList)
}